package client.user;

import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

public class UserEditDialog extends JDialog {
    
    private JTextField phoneField;
    private JTextField identityField;
    private JTextField realNameField;
    private JTextField addressField;
    private JDatePickerImpl birthDatePicker;
    private JComboBox<String> genderComboBox;
    private boolean confirmed = false;
    
    public UserEditDialog(String phone, String identity, String realName, String gender, String address, String birth) {
        setTitle("修改用户信息");
        setModal(true);
        setSize(400, 400);
        setLocationRelativeTo(null); // 确保窗口位于屏幕中心
        setLayout(new BorderLayout(10, 10));
        
        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(6, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        // 电话号码
        formPanel.add(new JLabel("电话号码:"));
        phoneField = new JTextField(phone);
        formPanel.add(phoneField);
        
        // 身份证号
        formPanel.add(new JLabel("身份证号:"));
        identityField = new JTextField(identity);
        formPanel.add(identityField);
        
        // 真实姓名
        formPanel.add(new JLabel("真实姓名:"));
        realNameField = new JTextField(realName);
        formPanel.add(realNameField);
        
        // 性别
        formPanel.add(new JLabel("性别:"));
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        genderComboBox.setSelectedItem(gender);
        formPanel.add(genderComboBox);
        
        // 住址
        formPanel.add(new JLabel("住址:"));
        addressField = new JTextField(address);
        formPanel.add(addressField);
        
        // 出生日期选择器
        formPanel.add(new JLabel("出生日期:"));
        birthDatePicker = createDatePicker(birth);
        formPanel.add(birthDatePicker);
        
        add(formPanel, BorderLayout.CENTER);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");
        
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (validateInput()) {
                    confirmed = true;
                    dispose();
                }
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private JDatePickerImpl createDatePicker(String birth) {
        UtilDateModel model = new UtilDateModel();
        if (birth != null && !birth.isEmpty()) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(birth);
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                model.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
                model.setSelected(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Properties p = new Properties();
        p.put("text.today", "Today");
        p.put("text.month", "Month");
        p.put("text.year", "Year");
        JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        return new JDatePickerImpl(datePanel, new DateLabelFormatter());
    }
    
    private boolean validateInput() {
        if (phoneField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "电话号码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (identityField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "身份证号不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if (realNameField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "真实姓名不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public boolean isConfirmed() {
        return confirmed;
    }
    
    public String getPhone() {
        return phoneField.getText().trim();
    }
    
    public String getIdentity() {
        return identityField.getText().trim();
    }
    
    public String getRealName() {
        return realNameField.getText().trim();
    }
    
    public String getGender() {
        return (String) genderComboBox.getSelectedItem();
    }
    
    public String getAddress() {
        return addressField.getText().trim();
    }
    
    public String getBirth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date selectedDate = (Date) birthDatePicker.getModel().getValue();
        return selectedDate != null ? sdf.format(selectedDate) : "";
    }
    
    // 日期格式化器
    public class DateLabelFormatter extends JFormattedTextField.AbstractFormatter {
        private String datePattern = "yyyy-MM-dd";
        private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
        
        @Override
        public Object stringToValue(String text) throws java.text.ParseException {
            return dateFormatter.parseObject(text);
        }
        
        @Override
        public String valueToString(Object value) throws java.text.ParseException {
            if (value != null) {
                Calendar cal = (Calendar) value;
                return dateFormatter.format(cal.getTime());
            }
            return "";
        }
    }
}
